<template>
    <div>
        <van-field
  v-model="fieldValue"
  is-link
  readonly
  label="地区"
  placeholder="请选择所在地区"
  @click="show = true"
/>
<van-popup v-model:show="show" round position="bottom">
  <van-cascader
    v-model="cascaderValue"
    title="请选择所在地区"
    :options="options"
    @close="show = false"
    @finish="onFinish"
  />
</van-popup>

        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                    :rules="[{ pattern, message: '请填写用户名' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
                <van-field v-model="sms" center clearable label="短信验证码" placeholder="请输入短信验证码"
                    :rules="[{ required: true, message: '请填写短信验证码' }]">
                    <template #button>
                        <van-button size="small" type="primary" @click="yzm" :disabled="time>0?true:false">
                            {{ time>0?time+'秒':'重新开始' }}
                        </van-button>
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>

    </div>
</template>

<script setup>
import router from '@/router';
import { ref } from 'vue';
import { showSuccessToast, showFailToast } from 'vant';
import { loginApp } from '@/api/api';
const username = ref('');
const password = ref('');
const sms=ref('')
let time=ref(0)
let timer
let pattern=/^1[3-9]\d{9}$/
const onSubmit = () => {
    loginApp(username.value,password.value).then(res=>{
        if(res.data.code==0){
            showSuccessToast('成功文案');
            router.push('/home/cara')
        }else{
            showFailToast('失败文案');
        }
    })
};
const yzm=()=>{
    sms.value=Math.floor(Math.random()*1000000)
    if(time>0)return
    time.value=5
    timer=setInterval(()=>{
        time.value--
        if(time.value==0){
            clearInterval(timer)
        }
    },1000)
}
const show = ref(false);
    const fieldValue = ref('');
    const cascaderValue = ref('');
    // 选项列表，children 代表子选项，支持多级嵌套
    const options = [
      {
        text: '浙江省',
        value: '330000',
        children: [{ text: '杭州市', value: '330100' }],
      },
      {
        text: '江苏省',
        value: '320000',
        children: [{ text: '南京市', value: '320100' }],
      },
    ];
    // 全部选项选择完毕后，会触发 finish 事件
    const onFinish = ({ selectedOptions }) => {
      show.value = false;
      fieldValue.value = selectedOptions.map((option) => option.text).join('/');
    };
</script>

<style lang="scss" scoped></style>